package com.wc.AlgoOJ_train.train2_DP训练.c4.DP0205;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/13 1:51
 * @description 滑雪
 * http://43.138.190.70:8888/p/DP0205
 */
public class Main {
    static int maxN = 500 + 7;
    static int[][] dp = new int[maxN][maxN];
    static int[][] ground = new int[maxN][maxN];
    static int[] posX = new int[]{-1, 0, 1, 0};
    static int[] posY = new int[]{0, -1, 0, 1};
    static int n, m;
    static int ans = 1;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);


    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                ground[i][j] = sc.nextInt();
                dp[i][j] = 1;
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                ans = Math.max(dfs(i, j), ans);
            }
        }
        out.println(ans);
        out.flush();
    }

    static int dfs(int x, int y) {
        // 已经计算过了说明
        if (dp[x][y] != 1) {
            return dp[x][y];
        }
        for (int i = 0; i < 4; i++) {
            int nx = x + posX[i];
            int ny = y + posY[i];
            if (!isBorder(nx, ny) && ground[nx][ny] < ground[x][y]) {
                dp[nx][ny] = dfs(nx, ny);
                dp[x][y] = Math.max(dp[x][y], dp[nx][ny] + 1);
            }
        }
        return dp[x][y];
    }

    static boolean isBorder(int x, int y) {
        return x < 1 || x > n || y < 1 || y > m;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }
}